<template>
  <el-dialog
    style="text-align: center"
    title="修改商品"
    width="width:600px"
    :visible.sync="dialogVisible"
  >
    <el-form ref="form" :model="form" label-width="80px" style="width: 500px">
      <el-form-item label="商品名称">
        <el-input v-model="form.FoodName" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="商品编号">
        <el-input v-model="form.FoodCode"></el-input>
      </el-form-item>
      <el-form-item label="商品类别">
        <el-input v-model="form.FoodCategory"></el-input> </el-form-item
      ><el-form-item label="商品价格">
        <el-input v-model="form.FoodPrice"></el-input>
      </el-form-item>
      <el-form-item label="商品销量">
        <el-input v-model="form.FoodQuantity"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onEditSave">保存</el-button>
        <el-button type="primary" @click="onCloseSave">关闭</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getFoodInfo, saveFoodInfo } from "@/api/food";
export default {
  components: {},
  data() {
    return {
      dialogVisible: false,
      form: {
        FoodName: "",
        FoodCode: "",
        FoodCategory: "",
        FoodPrice: "",
        FoodQuantity: "",
      },
    };
  },
  methods: {
    onOpen(foodId) {
      let data = { foodId: foodId };
      getFoodInfo(data).then((rsp) => {
        if (rsp.code === 0) {
          this.form = rsp.data;
        } else {
          this.$message.error(rsp.msg);
        }
      });
      this.dialogVisible = true;
    },
    onEditSave() {
      if (this.form.FoodName == "") {
        this.$message.error("商品名称不能为空");
        return;
      }
      if (this.form.FoodCode == "") {
        this.$message.error("商品编号不能为空");
        return;
      }
      if (this.form.FoodCategory == "") {
        this.$message.error("商品类别不能为空");
        return;
      }
      if (this.form.FoodPrice == "") {
        this.$message.error("商品价格不能为空");
        return;
      }
      if (this.form.FoodQuantity == "") {
        this.$message.error("商品销量不能为空");
        return;
      }
      let data = this.form;
      saveFoodInfo(data).then((rsp) => {
        if (rsp.code === 0) {
          this.$message.success("修改成功!");
          this.dialogVisible = false;
          this.$emit("onRefresh");
        } else {
          this.$message.error(rsp.msg);
        }
      });
    },
    onCloseSave() {
      this.dialogVisible = false;
    }
  },
};
</script>
